// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.entities;

import java.util.Collection;

import lichen.hibernate.HibernateSessionFactory;
import lichen.internal.hibernate.HibernateSessionFactoryImpl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry5.ioc.internal.services.ClassNameLocatorImpl;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.test.TapestryTestCase;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeMethod;

/**
 * 基础的模型测试
 * 
 * @author <a href="mailto:jun.tsai@gmail.com">Jun Tsai</a>
 * @version $Revision: 225 $
 * @since 0.0.1
 */
public class BaseModelTestCase extends TapestryTestCase {
	private final Logger _log = LoggerFactory.getLogger(BaseModelTestCase.class);
	private Session session;
	private Transaction transaction;

	@BeforeMethod
	public void setUpSession() {
		Collection<String> packageNames = CollectionFactory
				.newList("lichen.entities");

		HibernateSessionFactory source = new HibernateSessionFactoryImpl(_log,
				packageNames, new ClassNameLocatorImpl(null));
		session = source.getSessionFactory().openSession();
		session.setFlushMode(FlushMode.COMMIT);
	}
	protected Session getSession(){
		return session;
	}
	protected void beginTransaction(){
		transaction=session.beginTransaction();
	}
	protected void commit(){
		transaction.commit();
	}
	protected void flushAndClear(){
		session.flush();
		session.clear();
		
	}
}
